package caidan;

import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.SwingUtilities;

import caidan.data.FoodData;
import caidan.res.Resource;
import jinyilw.swing.ComponentTools;
import jinyilw.swing.rlaf.utils.RBorderFactory;
import jinyilw.swing.visuals.comp.ActImageComp;
import jinyilw.swing.visuals.panel.Position;
import jinyilw.swing.visuals.panel.PositionSetter;

public class FoodPanel extends FoodComp implements PositionSetter
{
	private static final long serialVersionUID = 2643962497715625147L;
	private ActImageComp deleteLabel;
	private CategoryPanel categoryPanel;
	private Position position;
	private volatile boolean choosed;
	private boolean editable;

	public FoodPanel(FoodData fData, CategoryPanel categoryPane)
	{
		super(fData, categoryPane.positionConfiger);
		categoryPanel = categoryPane;
		RBorderFactory.setToActiveBorder(this);
		addClickOperater((Object o) ->
		{
			if (editable)
			{
				SettingView settingView = MainApp.getSettingView();
				settingView.setView(this);
				settingView.setVisible(true);
				return;
			}
			MouseEvent e = (MouseEvent) o;
			if (e.getY() < (getHeight()
					- categoryPanel.positionConfiger.textHeight)
					&& categoryPanel.mainFrame.clickToLager)
			{
				LagerView lagerView = MainApp.getLagerView();
				lagerView.setView(this);
				lagerView.setVisible(true);
			} else
				chooseAct();
		});
	}

	public synchronized void chooseAct()
	{
		if (getImage() == null)
			setImage(foodData.getThumbnail());
		if (editable)
			return;
		if (!choosed)
		{
			categoryPanel.choose(this);
		} else
		{
			categoryPanel.unchoose(this);
		}
	}

	public void choose()
	{
		choosed = true;
		foodNameLabel.setText(foodData.getFoodName() + " √");
		//		if (MainApp.getLagerView().isVisible())
		//			MainApp.getLagerView().update();
	}

	public void unchoose()
	{
		choosed = false;
		foodNameLabel.setText(foodData.getFoodName());
		// moveTo(position);
		//		if (MainApp.getLagerView().isVisible())
		//			MainApp.getLagerView().update();
	}

	public void clear()
	{
		choosed = false;
		foodNameLabel.setText(foodData.getFoodName());
		setLocation(position.getX(), position.getY());
	}

	public boolean isChoosed()
	{
		return choosed;
	}

	public boolean isMaxChoosed()
	{
		return categoryPanel.isMaxChoosed();
	}

	public boolean isEditable()
	{
		return editable;
	}

	public void setChoosed(boolean b)
	{
		choosed = b;
	}

	@Override
	public void setPosition(Position p)
	{
		position = p;
		if (p != null)
			setBounds(p.getX(), p.getY(), p.getW(), p.getH());
	}

	@Override
	public void updatePosition()
	{
		if (position != null && !choosed)
			setBounds(position.getX(), position.getY(), position.getW(),
					position.getH());
	}

	public void moveTo(Position p)
	{
		ComponentTools.moveComponent(this, p.getX(), p.getY(), 35);
	}

	public void moveToInThread(Position p)
	{
		ComponentTools.moveComponentInThread(this, p.getX(), p.getY(), 35);
	}

	public void moveTo(Point p)
	{
		ComponentTools.moveComponent(this, (int) p.getX(), (int) p.getY(), 35);
	}

	public Point getConvertedPoint()
	{
		return SwingUtilities.convertPoint(categoryPanel.contentContainer,
				position.getX(), position.getY(), categoryPanel.glassPane);
	}

	public void count(int d, int m)
	{
		foodData.count(d, m);
	}

	public int getCount(int d, int m)
	{
		return foodData.getCount(d, m);
	}

	public int getTotalCount()
	{
		return foodData.getTotalCount();
	}

	public String getFoodName()
	{
		return foodData.getFoodName();
	}

	public Image getFoodImage()
	{
		return foodData.getFoodImage();
	}

	public String getImageNotFindMessege()
	{
		return foodData.getImageNotFindMessege();
	}

	public ArrayList<FoodPanel> getCurrentList()
	{
		return categoryPanel.getCurrentPanels();
	}

	public ArrayList<FoodData> getCurrentFoodDatas()
	{
		return categoryPanel.getCurrentFoodDatas();
	}

	public void setEditable(boolean editable)
	{
		if (this.editable == editable)
			return;
		this.editable = editable;
		if (editable)
		{
			if (deleteLabel == null)
			{
				deleteLabel = new ActImageComp(Resource.deleteIcon);
				deleteLabel.setLocation(getWidth() - deleteLabel.getWidth() - 2,
						0);
				deleteLabel.addClickOperator((Object o) ->
				{
					deleteThis();
				});
			}
			add(deleteLabel);
			setComponentZOrder(deleteLabel, 0);
		} else
		{
			remove(deleteLabel);
		}
	}

	public void deleteThis()
	{
		categoryPanel.delete(this);
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		int w = getWidth(), h = getHeight();
		super.setBounds(x, y, width, height);
		if (w == width && h == height)
			return;
		if (deleteLabel != null)
			deleteLabel.setBounds(width - 17, 0, 15, 15);
	}
}
